#include "RS232.h"
#include "../ACUAS/ACUAS.h"

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "../ACUAS/UART/uart.h"

#define UART_BAUD_RATE 1200

void
rs232_initialize()
{
  uart_init(UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU));
  sei();
}

unsigned char
rs232_get_command()
{
  unsigned char in = uart_getc();
  return (in >= 'A' && in <= 'Z') ? in : 0;
}

unsigned int
rs232_get_uint()
{
  delay_ds(1);
  return (uart_getc() << 8) + uart_getc();
}

unsigned char
rs232_get_uchar()
{
  delay_ds(1);
  return uart_getc();
}

void
rs232_send_uint(unsigned int value)
{
  uart_putc(value >> 8);
  uart_putc(value);
}

void
rs232_send_bet(unsigned int value)
{
  uart_putc('B');
  rs232_send_uint(value);
}

void
rs232_send_hit_or_stand(unsigned char hit)
{
  uart_putc('H');
  uart_putc(hit ? 'h' : 's');
}

void
rs232_skip()
{
  uart_putc('X');
}
